<?php
if (! defined('BASEPATH')) exit('No direct script access allowed');

class Frontend_Controller extends MY_Controller
{
    function __construct (){
        parent::__construct();
        $this->load->model('login/login_model');
        
        $this->init_login();
        
    }
    
    function init_login() {
        if (isset($_COOKIE['remember_login']) && !check_login()) {
            $remember_expire = 60 * 60 * 24 * 7; //7 ngay
            $arr_remember = array();
            $arr_remember_temp = json_decode($_COOKIE['remember_login'], TRUE);
            foreach ($arr_remember_temp as $key => $value) {
                $arr_remember[$key] = decrypt($value);
                if ($key == 'expire') {
                    $arr_remember[$key] = intval(str_replace('#', '', decrypt($value)));
                }
            }
            
            if ((time() - $arr_remember['expire']) < $remember_expire && !isset($_SESSION['member']['id'])) {
                $member_info = $this->login_model->get_by_email_password($arr_remember['email'], $arr_remember['password']);
                if ($member_info['id'] && !isset($_SESSION['member']['id'])) {
                    $_SESSION['member'] = $member_info;
                    $this->login_model->update_login($member_info['id']);
                }
            }
        }
    }
    
}
